const Service = require('egg').Service;

class PageService extends Service {
  async find(id) {
    const data = await this.app.mysql.query('select * from page where id = ?', id);
    const page = data && data[0] || null
    if (!page) {
        return null;
    }
    page.seed = JSON.parse(page.seed);
    page.seed.moduleList = (page.seed.moduleList || []).map((v, i) => {
      const data = require(`${this.app.baseDir}/app/public/${v.module_name}/${v.edition}/daily/schema.json`);
      return {
        ...v,
        schema: data
      };
    });
    let pageData = await this.app.mysql.select('page_data', { 
        where: { page_id: id },
        orders: [['order_num','asc']]
     });
    page.pageData = pageData;
    return page
  }
}

module.exports = PageService;